package com.hundsun.monitor.client;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RootPanel;
import com.hundsun.monitor.client.service.MonitorErrorLogService;
import com.hundsun.monitor.client.service.MonitorErrorLogServiceAsync;
import com.hundsun.monitor.shared.model.MonitorErrorLog;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MonitorHome implements EntryPoint {

	private MonitorErrorLogServiceAsync serviceAsync = GWT.create(MonitorErrorLogService.class);

	private FlexTable listContent = new FlexTable();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// set uncaught exception handler
		GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {

			@Override
			public void onUncaughtException(Throwable throwable) {
				String text = "Uncaught exception: ";
				while (throwable != null) {
					StackTraceElement[] stackTraceElements = throwable.getStackTrace();
					text += throwable.toString() + "\n";
					for (StackTraceElement element : stackTraceElements) {
						text += "    at " + element + "\n";
					}
					throwable = throwable.getCause();
					if (throwable != null) {
						text += "Caused by: ";
					}
				}
				DialogBox dialogBox = new DialogBox(true, false);
				dialogBox.getElement().getStyle().setProperty("backgroundColor", "#ABCDEF");
				System.err.print(text);
				text = text.replaceAll(" ", "&nbsp;");
				dialogBox.setHTML("<pre>" + text + "</pre>");
				dialogBox.center();
			}
		});

		// use a deferred command so that the handler catches onModuleLoad2()
		// exceptions
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				onModuleLoad2();
			}

		});
	}

	private void onModuleLoad2() {
		RootPanel rootPanel = RootPanel.get();
//		final PortalLayout portalLayout = new PortalLayout(1);
//		portalLayout.setWidth100();
//		portalLayout.setHeight100();
//
//		Portlet portlet = new Portlet();
//		portlet.setTitle("监视器异常日志列表");

		listContent.removeAllRows();
		listContent.setText(0, 0, "监视器名称");
		listContent.setText(0, 1, "监视器类型");
		listContent.setText(0, 2, "主机地址");
		listContent.setText(0, 3, "发生时间");
		listContent.setText(0, 4, "异常内容");
		listContent.getRowFormatter().addStyleName(0, "watchListHeader");
		listContent.addStyleName("watchList");
		serviceAsync.getErrorLogPagination(new MonitorErrorLog(), new AsyncCallback<List<MonitorErrorLog>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(List<MonitorErrorLog> result) {
				for (MonitorErrorLog data : result) {
					int row = listContent.getRowCount();
					listContent.setText(row, 0, data.getMonitorname());
					listContent.setText(row, 1, data.getDisplaytypename());
					listContent.setText(row, 2, data.getHost());
					listContent.setText(row, 3, new Date(data.getCreatetime()).toLocaleString());
					listContent.setText(row, 4, data.getContent());
				}
			}
		});
//		portlet.addItem(listContent);
//
//		portalLayout.addChild(portlet);
		rootPanel.add(listContent);
	}

}
